<?php
require_once 'uhet.php';

class Dao extends Uhet
{
	protected $db;
	protected $dbConfig;
		
	public function __construct(){
		
		parent::__construct(substr(get_class($this),3));

		require_once dirname(__FILE__) . '/../db/conexao.php'; 
		$this->db = new Conexao();
		$this->db->conectar();
		
		require_once dirname(__FILE__) . '/../db/dbconfig.php';
		$this->dbConfig = new dbConfig($this->getTableName());		
	}

	public function getCount(){
		
		$sql = 'SELECT COUNT(*) FROM ' . $this->getTableName();
		$result = $this->db->query($sql);
		$linha = mysql_fetch_row($result);
		return $linha[0];
	}

	public function inserir(&$object){
		
		$atributos = $object->getArrayAtributos();
		$sql = 'INSERT INTO ' . $this->getTableName() . '(';
		
		foreach (array_keys($atributos) as $index) $sql .= $index.',';
		$sql[strrpos($sql,',')] = ')';
		$sql .= ' VALUES(';
		foreach ($atributos as $atributo) $sql .= "'" . $atributo . "',";
		$sql[strrpos($sql,',')] = ')';
		$this->db->query($sql);
		$object->setId($this->db->get_id());
				
		$matriz_objetos = $object->getMatrizObjetos();
		if ($matriz_objetos) {
			foreach ($matriz_objetos as $array_objetos){
				
				foreach ($array_objetos as $objeto) $this->inserir($objeto);
			}
		}
	}

	public function findall(){
		
		$sql = 'SELECT * FROM ' . $this->getTableName();
		$result = $this->db->query($sql);
		//echo $sql . "\n";
		
		$object_name = $this->getObjectName();
		//echo $object_name;
		$objectArray = array();
		//print_r(get_included_files());
		while($linha = mysql_fetch_assoc($result)){
		
			//$object = new $object_name;
			$object = $this->createObject();
			$object->loadArray($linha); 
			$objectArray[] = $object;			
		
		}
		return $objectArray;
	}
	
	public function consultar($id){

		if ($id) {
			
			$sql  = 'SELECT * ';
			$sql .= 'FROM ' . $this->getTableName() . ' ';
			$sql .= 'WHERE id='."'$id'";
			$result = $this->db->query($sql);
			
			$object = $this->createObject();
			
			$object->loadArray(mysql_fetch_assoc($result));
			return $object;
		}
		
		return null;
	}
	
/*	public function getCampo($campo,$valor){

		if ($campo) {			
			$sql  = 'SELECT * ';
			$sql .= 'FROM ' . $this->getTableName() . ' ';
			$sql .= 'WHERE ' . $campo .' = ' ."'$valor'";
			
			$result = $this->db->query($sql);			
			$object = $this->createObject();
			
			$object->loadArray(mysql_fetch_assoc($result));
			return $object;
		}		
		return null;
	}*/
	
	
/*
 * order by
 * desc  -> asc desc
 * limit -> ini
 *       -> qtd
 * 
 * SELECT * FROM `materia` ORDER BY id DESC LIMIT 2,2
 *
 */

/*
 * array[ordenacao] = id
 * array[orientacao] = crescende | decrecente 
 * array[inicio] = 0..n
 * array[quantidade] = 1..n
 */

	public function find($dbconfig){
		
		if ($dbconfig){
			$sql  = 'SELECT * ';
			$sql .= 'FROM ' . $this->getTableName() . ' ';
			$sql .= 'ORDER BY ' . $dbconfig->get('ordenacao') . ' ';
			$sql .= $dbconfig->get('orientacao') . ' ';
			$sql .= 'LIMIT ' . $dbconfig->get('inicio') . ' , ' . $dbconfig->get('quantidade');
			
//			echo $sql . "\n";
//			print_r($dbconfig);
			
			$result = $this->db->query($sql);
			
			$object_name = $this->getObjectName();
			$objectArray = array();
			while($linha = mysql_fetch_assoc($result)){
		
				$object = new $object_name;
				$object->loadArray($linha); 
				$objectArray[] = $object;
			}
			
			//echo $sql . "\n";		
			return $objectArray;

		} else die('model::find($dbconfig) $dbconfig esta configurado corretamente?');
	}
}
?>